package com.yh.hljapp.gddb.gbmanager;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;

import com.yh.hljapp.AppContext;
import com.yh.hljapp.gddb.gdbean.DaoMaster;
import com.yh.hljapp.gddb.gdbean.DaoSession;
import com.yh.hljapp.gddb.gdbean.MatHeadBean;
import com.yh.hljapp.gddb.gdbean.MatHeadBeanDao;

import java.util.List;

/**
 * =========================
 * 版权所有，违法必究
 * 作者：john
 * <p>
 * 工程：FoodBarcode
 * <p>
 * 文件名：MatHeadManager
 * <p>
 * 创建时间：2017.05.25.9:07
 * *****************************
 */
public class MatHeadManager {

    private final static String dbName = "fb_db";
    private static MatHeadManager mInstance;
    private DaoMaster.DevOpenHelper openHelper;
    private Context context;
    private final DaoSession mDaoSession;

    public MatHeadManager(Context context) {
        this.context = context;
        openHelper = new DaoMaster.DevOpenHelper(context, dbName, null);
        DaoMaster daoMaster = new DaoMaster(getWritableDatabase());
        mDaoSession = daoMaster.newSession();
    }

    /**
     * 获取单例引用
     *
     * @param context
     * @return
     */
    public static MatHeadManager getInstance(Context context) {
        if (mInstance == null) {
            synchronized (MatHeadManager.class) {
                if (mInstance == null) {
                    mInstance = new MatHeadManager(context);
                }
            }
        }
        return mInstance;
    }

    /**
     * 获取可读数据库
     */
    private SQLiteDatabase getReadableDatabase() {
        if (openHelper == null) {
            openHelper = new DaoMaster.DevOpenHelper(context, dbName, null);
        }
        SQLiteDatabase db = openHelper.getReadableDatabase();
        return db;
    }

    /**
     * 获取可写数据库
     */
    private SQLiteDatabase getWritableDatabase() {
        if (openHelper == null) {
            openHelper = new DaoMaster.DevOpenHelper(context, dbName, null);
        }
        SQLiteDatabase db = openHelper.getWritableDatabase();
        return db;
    }


    public void insertOrReplace(MatHeadBean matHeadBean) {
        String name1 = AppContext.getInstance().getCurrentUser().getName();
        String ftype = matHeadBean.getFtype();
        String name = matHeadBean.getName();
        matHeadBean.setUserName(name1);
        MatHeadBean unique = mDaoSession.getMatHeadBeanDao().queryBuilder().where(MatHeadBeanDao.Properties.Name.eq(name), MatHeadBeanDao.Properties.Ftype.eq(ftype), MatHeadBeanDao.Properties.UserName.eq(name1)).unique();
        if (unique==null){
            MatHeadBeanDao headBeanDao = mDaoSession.getMatHeadBeanDao();
            headBeanDao.insert(matHeadBean);
        }else{
            unique.setValue(matHeadBean.getValue());
            mDaoSession.getMatHeadBeanDao().update(unique);
        }
    }

    public long getCount() {
        MatHeadBeanDao headBeanDao = mDaoSession.getMatHeadBeanDao();
        return headBeanDao.count();
    }

    public String getHeadValue(String name,String type) {//type 2代表销售出库，3代表退货
        String userName = AppContext.getInstance().getCurrentUser().getName();
        MatHeadBeanDao headBeanDao = mDaoSession.getMatHeadBeanDao();
        MatHeadBean unique = headBeanDao.queryBuilder().where(MatHeadBeanDao.Properties.Name.eq(name),MatHeadBeanDao.Properties.Ftype.eq(type),MatHeadBeanDao.Properties.UserName.eq(userName)).unique();
        if (unique != null){
            return unique.getValue();
        }
        return "";
    }

    public void delAllMatHead() {
        String name = AppContext.getInstance().getCurrentUser().getName();
        MatHeadBeanDao headBeanDao = mDaoSession.getMatHeadBeanDao();
        List<MatHeadBean> list = headBeanDao.queryBuilder().where(MatHeadBeanDao.Properties.UserName.eq(name)).list();
        headBeanDao.deleteInTx(list);
//        headBeanDao.queryBuilder().where(MatHeadBeanDao.Properties.UserName.eq(name)).buildDelete().forCurrentThread();
    }



    public String getIDByName(String name,String type) {//type 2代表销售出库，3代表退货
        String userName = AppContext.getInstance().getCurrentUser().getName();
        MatHeadBeanDao headBeanDao = mDaoSession.getMatHeadBeanDao();
        MatHeadBean unique = headBeanDao.queryBuilder().where(MatHeadBeanDao.Properties.Name.eq(name),MatHeadBeanDao.Properties.Ftype.eq(type),MatHeadBeanDao.Properties.UserName.eq(userName)).unique();
        if (unique != null){
            return unique.getId();
        }
        return "";
    }


    public String getValueByPosition(String position,String type) {
        MatHeadBeanDao headBeanDao = mDaoSession.getMatHeadBeanDao();
        MatHeadBean unique = headBeanDao.queryBuilder().where(MatHeadBeanDao.Properties.Position.eq(position),MatHeadBeanDao.Properties.Ftype.eq(type)).unique();
        if (unique != null){
            return unique.getValue();
        }
        return "";
    }


    public String getIDByPosition(String position) {
        MatHeadBeanDao headBeanDao = mDaoSession.getMatHeadBeanDao();
        MatHeadBean unique = headBeanDao.queryBuilder().where(MatHeadBeanDao.Properties.Position.eq(position)).unique();
        if (unique != null){
            return unique.getId();
        }
        return "";
    }

}
